<template>
	<view class="contont">
		<nav-bar :title="title" :iconLeft="true" />
		<view class="content-bgc">
			<view class="b-con">
				<view style="margin-top: 20rpx;">
					<up-cell-group :border="false">
						<up-cell title="采购单号">
							<template #value>
								<view class="cell-text">{{ detail_record.code }} </view>
							</template>
						</up-cell>
						<up-cell title="采购计划单号">
							<template #value>
								<view class="cell-text">{{ detail_record.plan_code }} </view>
							</template>
						</up-cell>
						<up-cell title="采购员">
							<template #value>
								<view class="cell-text">{{ detail_record.purchase_full_name }} </view>
							</template>
						</up-cell>
						<up-cell title="下单日期">
							<template #value>
								<view class="cell-text">{{ detail_record.order_date }} </view>
							</template>
						</up-cell>
						<up-cell title="交货日期">
							<template #value>
								<view class="cell-text">{{ detail_record.delivery_date }}</view>
							</template>
						</up-cell>
						<up-cell title="备注">
							<template #value>
								<view class="cell-text" style="width: 70%;">{{ detail_record.desc }}</view>
							</template>
						</up-cell>
					</up-cell-group>
				</view>
			</view>
			<view class="b-con">
				<up-cell-group :border="false">
					<up-cell :isLink="true" :border="false" :title="`物料：总数量（${car_list.length}）`" @click="car_popupShow = true">
						<template #value>
							<view class="cell-text">点击查看 </view>
						</template>
					</up-cell>
				</up-cell-group>
			</view>
			<custom-popup :popupShow="car_popupShow" mode="bottom" :title="'物料'" class="car-popup-custom"
				@close="car_popupShow = false" height="70vh">
				<z-paging v-if="car_popupShow" ref="paging_car" :refresher-enabled="false" :loading-more-enabled="false" :fixed="false" v-model="car_list"
					@query="queryList_car">
					<uni-card v-for="(item, index) in car_list" :key="index" :title="item.name" :sub-title="item.code"
						margin="5px" spacing="0">
						<view class="ctent">规格：{{ item.specification }}</view>
						<view class="con" style="align-items: normal;">
							<view class="ctent" style="flex: none;">库存：</view>
							<view class="ctent">{{ item.inventory_info }}</view>
						</view>
						<view class="con">
							<view class="ctent">损耗率：{{ item.loss }}%</view>
							<view class="ctent">供应商：{{ item.supplier_name }}</view>
						</view>
						<view class="con">
							<view class="ctent">采购单位：{{ item.p_unit }}</view>
							<view class="ctent">采购数量：{{ item.p_num }}</view>
						</view>
						<view class="con">
							<view class="ctent">总价：{{ item.gross_price }}</view>
							<view class="ctent">优惠率：{{ item.discount }}%</view>
						</view>
						<view class="ctent">最终价：{{ item.total_price }}</view>
					</uni-card>
				</z-paging>
			</custom-popup>
		</view>
	</view>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import puOrderApi from '../../../api/purchase/pu_order'

const detail_record = ref({})
const title = '采购详情';
onLoad(option => {
	get_detail(option.id);
})

function get_detail(id) {
	puOrderApi.purchase_order_detail(id).then(res => {
		detail_record.value = res
		car_list.value = res.purchase_order_boms.map(v => {
			const total = parseFloat(v.supplier_price) * v.num
			return {
				id: v.bom.id,
				code: v.bom.code,
				name: v.bom.name,
				specification: v.bom.specification,
				supplier_name: v.supplier_name,
				stock_info: v.bom.stock_info,
				loss: v.bom.loss,
				p_unit: v.unit,
				p_num: v.num,
				discount: parseFloat(v.discount),
				total_price: v.total_price,
				gross_price: parseFloat(total.toFixed(2)),
				inventory_info: v.bom.stock_info.map(v => (`${v.warehouse_name}【${v.unit_info}】`)).join('、'),
			}
		})
	})
}

const car_popupShow = ref(false)
const car_list = ref([])
const paging_car = ref(null)
function queryList_car() {
	paging_car.value.complete(car_list.value);
}
</script>
